<?php

namespace Xuan\Controller;

class AdsController extends CommonController {

    public function lists() {
        $sql = "1=1";
        $keywords = trim(I('get.keywords'));
        $pid = I('get.pid', '', 'int');
        if ($keywords) {
            $sql .= " AND title like '%" . $keywords . "%'";
        }
        if ($pid) {
            $sql .= " AND pid = '" . $pid . "'";
        }
        $count = M('ads')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('ads')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->assign("pid", $pid);
        $this->display();
    }

    public function detail() {
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $detail = M("ads")->where("id = " . $id . "")->find();
            if ($detail['is_start'] == 0) {
                $detail['starttime'] = date("Y-m-d");
                $detail['endtime'] = date("Y-m-d", strtotime("+1 day"));
            } else {
                $detail['starttime'] = date("Y-m-d", $detail['starttime']);
                $detail['endtime'] = date("Y-m-d", $detail['endtime']);
            }
        } else {
            $detail['starttime'] = date("Y-m-d");
            $detail['endtime'] = date("Y-m-d", strtotime("+1 day"));
            $detail['is_check'] = 1;
        }
        $cats = M("ads_cat")->select();
        $this->assign("cats", $cats);
        if ($id == '') {
            $detail['pid'] = $cats[0]['id'];
        }
        $cat = M("ads_cat")->where("id = " . $detail['pid'] . "")->find();
        $this->assign("cat", $cat);
        $this->assign("detail", $detail);
        $this->display();
    }

    public function detail_post() {
        $id = I('post.id', '', 'int');
        $path = C('picFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['logo'];
            if ($uploadInfo) {
                $data['logo'] = $uploadInfo['savename']; //数据库保存名
            }
        }

        $data['title'] = I('post.title');
        $data['pid'] = I('post.pid', '', 'int');
        $data['ord'] = I('post.ord', '30', 'int');
        $data['url'] = I('post.url');
        $data['is_check'] = I('post.is_check', 0, 'int');

        if ($id > 0) {
            M("ads")->where("id = " . $id . "")->save($data);

            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            M("ads")->add($data);
            $this->success('添加成功！', U('Ads/lists'));
        }
        clearTempFile();
    }

    public function delpic($id, $field, $table) {
        $table = I("post.table") ? I("post.table") : $table;
        $field = I("post.field") ? I("post.field") : $field;
        $id = I("post.id", 0, 'int') ? I("post.id") : $id;
        $info = M($table)->field($field)->where("id = " . $id . "")->find();
        if ($info) {
            M($table)->where("id = " . $id . "")->save(array($data[$field] => ''));
        }
    }

    public function cat() {
        $sql = "1=1";
        $keywords = trim(I('get.keywords'));
        if ($keywords) {
            $sql .= " AND title like '%" . $keywords . "%'";
        }
        $count = M('ads_cat')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('ads_cat')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->display();
    }
     public function cat_detail() {
        $id = I('get.id', '', 'int');
        if($id>0){
        $detail = M("ads_cat")->where("id = " . $id . "")->find();
        }
        $this->assign("detail", $detail);
        $this->display();
    }
    public function cat_detail_post() {
        $id = I('post.id', '', 'int');
        $data['title'] = I('post.title');
        $data['width'] = I('post.width', 0, 'int');
        $data['height'] = I('post.height', 0, 'int');
        $data['ord'] = I('post.ord', '30', 'int');
        if ($id > 0) {
            M("ads_cat")->where("id = " . $id . "")->save($data);
             $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            M("ads_cat")->add($data);
            $this->success('添加成功！', U('Ads/cat'));
        }
        clearTempFile();
    }

}

?>
